package com.bristua.mall.wx.biz;

import com.bristua.mall.common.utils.CacheUtil;
import com.bristua.mall.user.dao.CountUserConsumptionRecordMapper;
import com.bristua.mall.user.dao.TUserAccountMapper;
import com.bristua.mall.user.dao.TUserMapper;
import com.bristua.mall.user.model.TUser;
import com.bristua.mall.user.model.TUserAccount;
import com.bristua.mall.user.model.condition.TUserAccountExample;
import com.bristua.mall.user.model.condition.TUserExample;
import com.bristua.mall.wx.model.vo.QueryWxUserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2019/4/17.
 */
@Component
public class AppGetUserInfoBiz {
    @Autowired
    private TUserMapper tUserMapper;
    @Autowired
    private CacheUtil cacheUtil;
    @Resource
    private CountUserConsumptionRecordMapper countUserConsumptionRecordMapper;


    @Resource
    private TUserAccountMapper tUserAccountMapper;

    public QueryWxUserVO getUserInfo(){
        TUser userSession = (TUser) cacheUtil.getUserSession();
        TUserExample tUserExample = new TUserExample();
        tUserExample.createCriteria().andUserIdEqualTo(userSession.getUserId());
        List<TUser> tUsers = tUserMapper.selectByExample(tUserExample);
        if(tUsers!=null && tUsers.size()>0){
            TUser tUser = tUsers.get(0);
            QueryWxUserVO queryWxUserVO = new QueryWxUserVO();
            BeanUtils.copyProperties(tUser,queryWxUserVO);

            TUserAccountExample tUserAccountExample = new TUserAccountExample();
            tUserAccountExample.createCriteria().andUserIdEqualTo(userSession.getUserId());//用户ID

            List<TUserAccount> tUserAccounts = tUserAccountMapper.selectByExample(tUserAccountExample);

            if(tUserAccounts!=null && tUserAccounts.size()>0){
                queryWxUserVO.setBalanceAmt(String.valueOf(tUserAccounts.get(0).getUserIntegral()));
//                result.put("balanceAmt",);
            }else{
                queryWxUserVO.setBalanceAmt("0");
            }

            Integer integer = countUserConsumptionRecordMapper.countuserConsumptionRecord(userSession.getUserId());

            queryWxUserVO.setTotalConsumeAmt(integer==null?"0":String.valueOf(integer));

            TUserExample tUserExample1 = new TUserExample();
            tUserExample1.createCriteria().andParentIdEqualTo(userSession.getUserId());

            List<TUser> tUsers1 = tUserMapper.selectByExample(tUserExample1);

            if(tUsers1==null || tUsers1.size()==0){
                queryWxUserVO.setInviteNum(0);
            }else{
                queryWxUserVO.setInviteNum(tUsers1.size());
            }
            return queryWxUserVO;
        }
        return null;
    }
}
